﻿<%@ WebHandler Language="C#" Class="UserRights" %>

using System;
using System.Web;
using System.Web.SessionState;
using System.Collections.Generic;
using GONES.Model;
public class UserRights : IHttpHandler, IRequiresSessionState
{

    Spring.Context.IApplicationContext appContext = Spring.Context.Support.ContextRegistry.GetContext();
    GONES.Tools.Common.LoginInfo loginInfo = new GONES.Tools.Common.LoginInfo();
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string title = context.Request["title"];

        if (context.Session["LoginInfo"] != null)
        {
            loginInfo = (GONES.Tools.Common.LoginInfo)context.Session["LoginInfo"];
        }
        if (string.IsNullOrEmpty(title))
        {
            failure("无指定操作");
        }
        else
        {
            switch (title)
            {
                case "UserRoleSet":
                    if (UserRoleSet(context.Request["uid"],context.Request["uroleidstr"]))
                    {
                        context.Response.Write(success("", "UserRights"));
                    }
                    else
                    {
                        context.Response.Write(failure("数据库连接错误"));
                    }
                    break;
                case "UserPermisionSet":
                    if (UserPermisionSet(context.Request["uid"], context.Request["navPermissionId"]))
                    {
                        context.Response.Write(success("", "UserRights"));
                    }
                    else
                    {
                        context.Response.Write(failure("数据库连接错误"));
                    }
                    break;
            }
        }
    }

    protected bool UserPermisionSet(string uid, string navpermisionIdstr)
    {
        if (navpermisionIdstr == "1")
            return true;
        try
        {
            var unpManager = appContext.GetObject("S_UserNavpermissionManager") as GONES.IBLL.IS_UserNavpermissionManager;
            IList<S_UserNavpermission> unpList = unpManager.GetUserNavPermissionByUid(uid);
            S_UserNavpermission unavModel = null;
            foreach (var unp in unpList)
            {
                unpManager.Delete(unp.ID);
            }
            string[] navpermisionList = navpermisionIdstr.Split('|');
            for (int i = 0; i < navpermisionList.Length; i++)
            {
                unavModel = new S_UserNavpermission();
                unavModel.ID = GONES.Tools.Common.Rnd.Guid;
                unavModel.NavPermissionId = navpermisionList[i].Trim();
                unavModel.Uid = uid;
                unpManager.Add(unavModel);
            }
            return true;
        }
        catch
        {
            return false; 
        }
         
    }

    protected bool UserRoleSet(string uid, string roleidstr)
    {
        if (roleidstr == "1")
            return true;
        var ruManager = appContext.GetObject("S_UserRoleManager") as GONES.IBLL.IS_UserRoleManager;
        var unpManager = appContext.GetObject("S_UserNavpermissionManager") as GONES.IBLL.IS_UserNavpermissionManager;
        GONES.Model.S_UserRole userRoles = null;
        try
        {
            IList<S_UserRole> userrolesList = ruManager.GetUserRolesList(uid);
            IList<S_UserNavpermission> unpList = unpManager.GetUserNavPermissionByUid(uid);
            foreach (var roleUser in userrolesList)
            {
                ruManager.Delete(roleUser.ID);
            }
            foreach (var unp in unpList)
            {
                unpManager.Delete(unp.ID);
            }
            if (roleidstr != "")
            {
                string[] roleidlist = roleidstr.Split(',');
                for (int i = 0; i < roleidlist.Length; i++)
                {
                    userRoles = new S_UserRole();
                    userRoles.Uid = uid;
                    userRoles.RoleId = roleidlist[i].Trim();
                    userRoles.ID = GONES.Tools.Common.Rnd.Guid;
                    ruManager.Add(userRoles);
                    AddRoleNavPermision(userRoles.RoleId, uid);
                }
            }
            return true;
        }
        catch
        {
            return false;
        }
    }

    protected void AddRoleNavPermision(string roleid,string userid)
    {
        var rolepermissionManager = appContext.GetObject("S_RolePermissionManager") as GONES.IBLL.IS_RolePermissionManager;
        var usernavpermissionManager = appContext.GetObject("S_UserNavpermissionManager") as GONES.IBLL.IS_UserNavpermissionManager;
        S_UserNavpermission unavModel = new S_UserNavpermission();
        IList<S_RolePermission> rpList= rolepermissionManager.GetRolePermissionList(roleid);
        foreach (var rp in rpList)
        {
            unavModel = new S_UserNavpermission();
            unavModel.ID = GONES.Tools.Common.Rnd.Guid;
            unavModel.NavPermissionId = rp.NavPermissionid;
            unavModel.Uid = userid;
            usernavpermissionManager.Add(unavModel);
        }
    }

    public string success()
    {
        return "{\"statusCode\":\"200\", \"message\":\"操作成功!\"}";
    }
    public string success(string str, string navTabId)
    {
        return "{\"statusCode\":\"200\", \"message\":\"操作成功!" + str + "\",\"navTabId\":\"" + navTabId + "\"}";
    }
    public string failure()
    {
        return "{\"statusCode\":\"300\", \"message\":\"操作失败\"}";
    }
    public string failure(string str)
    {
        return "{\"statusCode\":\"300\", \"message\":\"操作失败!" + str + "\"}";
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}